package cn.com.callback

import cn.com.kh.KHDBConfig
import cn.com.elite.EliteBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @Title: aipHSTRecycle_operation_timing
 * @Description:九级真美 满意度短信发送
 * @author: xrm
 * @date 2022-12-20 15:52:00
 *
 */

public class job_jjzm_SMS_sending implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(job_jjzm_SMS_sending.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {

        log.debug("-------------------------start 满意度短信发送-----------------");
        //and r1.DNIS REGEXP '^0?[1][35678][0-9]{9}$'
        try {

            //1.list查数据   查询有效的推送短信电话号码    电话号码  event_guid
            String selectSQL = '''select case when r1.calltype = '1' then right(r1.ani,11) when r1.calltype = '2' then right(r1.dnis,11) else '' end as mobile ,max(r1.event_guid) as event_guid ,max(r1.customer_guid) as customer_guid ,max(r2.statisfaction1) as statisfaction1
                                    from event r1 
                                    left join wmsk_statisfaction r2 on r1.event_guid = r2.EVENT_GUID 
                                    left join jm_satisfaction_list r3 on r1.event_guid = r3.event_guid
                                    left join TABCcalsum r4 on r1.event_guid = r4.event_guid   
                                    left join tabccalsm_list r5 on r4.relation_id = r5.parent_id 
                                     where r1.calltype in ('1','2') 
                                     and r1.callestablished = 1  
                                     and (r2.Event_GUID is null or r2.statisfaction1='3' or r2.statisfaction1='4' or r2.statisfaction1='0')
                                     and r3.rec_guid is null
									 and r4.event_guid is not  null
                                     and  (len(r1.DNIS)>=11 or len(r1.ani)>=11)
                                     and isnull(r5.callnature2_id,'') !='200093'
									 and isnull(r5.callnature3_id,'') !='300192' 
                                     and r1.CALLSTARTTIME >= CONVERT(varchar(100),GETDATE(),23)
									 and r1.callendtime <=dateadd(minute,-3,GETDATE())
									 and r1.callendtime is not null

                                    group by case when r1.calltype = '1' then right(r1.ani,11) when r1.calltype = '2' then right(r1.dnis,11) else '' end''';

            List eventlist = eliteBean.chaxun_sql(selectSQL, "dataSource");

            int eventlistNum = eventlist.size();
            if (eventlistNum > 0) {
                // 获取系统参数配置的满意度模板内容
                String sendInfo = KHDBConfig.getParamConfig(eliteBean, "STASIN");
                log.debug("满意度短信发送-获取系统参数配置的满意度模板内容sendInfo:" + sendInfo);
                for (int i = 0; i < eventlist.size(); i++) {
                    String mobile = eventlist.get(i).get("mobile");
                    String event_guid = eventlist.get(i).get("event_guid");
                    String customer_guid = eventlist.get(i).get("customer_guid");
                    String statisfaction1 = eventlist.get(i).get("statisfaction1");
                    log.debug("满意度短信发送-当前信息：event_guid" + event_guid + " mobile：" + mobile + " customer_guid：" + customer_guid + " statisfaction1：" + statisfaction1);

                    //2.2 insert 短信表
                    String[] custTaskDealSQL=new String[2];
                    // 新增
                    // 发送记录
                   
                String satisfaction_type="";
				String satisfaction_value = "";
				List TodayList;
                if(statisfaction1=="3"||statisfaction1=="4"){
                    satisfaction_type="电话";
					satisfaction_value=statisfaction1;
                }else{
                    //查当前号码是否在今天已存在过发送短信任务,如果没有，才去发送短信
                    TodayList = eliteBean.chaxun_sql("select count(*) as todaysmsnum from jm_satisfaction_list where send_no='" + mobile + "' and satisfaction_type='短信' and CONVERT(varchar(100),send_time,23)=CONVERT(varchar(100),GETDATE(),23)","dataSource")
                    String todaysmsnum = TodayList[0].getAt("todaysmsnum");
                    if(todaysmsnum == "0"){
                        satisfaction_type="短信";
                        satisfaction_value ="";
                    }
                }
				
					//首次未评价OR一般OR不满意，写入数据，后续电话未评价，则不写入
					if(satisfaction_type=="短信"||satisfaction_type=="电话"){
					eliteBean.update("INSERT INTO jm_satisfaction_list (rec_guid, event_guid, send_no, send_time, send_content, reply_time, reply_content, satisfaction_type, satisfaction_value, createdtime) VALUES (NEWID(), '" + event_guid + "', '" + mobile + "', GETDATE(), '" + sendInfo + "', NULL, NULL, '" + satisfaction_type + "','" + satisfaction_value + "', GETDATE())","dataSource");
                    }
					
					if(satisfaction_type=="短信"){
						eliteBean.update("INSERT INTO DOCUMENTOUT ( DOCUMENTOUT_GUID, DOCTYPE, STATUS, TRIALCOUNT, TRIALCOUNTLIMIT, DOCUMENTPATH, DOCUMENTTO, HANDLEBY_ID, HANDLEGROUP_ID, CUSTOMER_GUID, MODIFIEDBY, MODIFIEDDATE, CREATEDBY, CREATEDDATE, ExpectedStartDate ) VALUES (NEWID(), 3, 0, 0, 3, '" + sendInfo + "','" + mobile + "', 'SELITE', 'SYSTEM', '" + customer_guid + "', 'SELITE',GETDATE(), 'SELITE', GETDATE(), GETDATE())","dataSource");
					}
                    
					//log.debug("满意度短信发送-SQL：" + custTaskDealSQL.toString());
					
                    //log.debug("满意度短信发送-SQL返回结果：" + batchSQLResult.toString());

                }
            } else {
                log.debug("满意度短信发送-无需要处理的待发送任务...");
            }

        } catch (Exception e) {
            log.error("满意度短信发送-接口处理异常" + e.getMessage());
        }

    }

}